const router = require('express').Router();
const connection = require('../database/mysql')
const path = require('path')
const sql = require('../routehandles/mysqlhandle')

router.post('/lostpsw',async function(req,res)
{
    var params = req.body
    console.log(params)
    let rule = /^1[345789]\d{9}$/  //是否为标准的电话号码
    var reg= /^[0-9a-zA-z-_]+$/
    var restOfTime = 3
    let rule1 = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/   //6-20位，包含大小写和数字
    if(params.username == null || params.username == '')
    {
        return res.json({msg:'请填写电话号码！',result:false})
    }else if (!rule.test(params.username))
    {
        console.log(params.username)
        return res.json({msg:'号码不正确！',result:false})
    }
    else if(params.password == '' || params.password2 == '')
    {
        console.log('1111')
        return res.json({msg:'密码不能为空！',result:false})
    }else if(!params.password.match(rule1) && !params.password2.match(rule1))
    {
        return res.json({msg:'密码格式不正确！',result:false})
    }else if(params.password != params.password2)
    {
        console.log('2222')
        return res.json({msg:'两次密码不一致！',result:false})
    }else if(params.verify == null || params.verify =='')
    {
        
        return res.json({msg:'请输入验证码',result:false})
    }else if(params.verify != params.code)
    {
        return res.json({msg:'验证码不正确',result:false,last:restOfTime})
     }
    else
    {
        console.log('6666')
        sql.uUAPTable('password',params.password,params.username);
        return res.json({msg:'密码重置成功！',result:true})
    }
})

module.exports = router
